//
//  PictureSelectorViewController.swift
//  SinaWeibo
//
//  Created by 张子国 on 15/11/22.
//  Copyright © 2015年 张子国. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let column : CGFloat = 4
private let pictureCellMargin: CGFloat  = 10

class PictureSelectorViewController: UICollectionViewController {
    init(){
        //指定layout
        let layout = UICollectionViewFlowLayout()
        let w = (screenW - (column + 1) * pictureCellMargin) / column
        layout.itemSize = CGSize(width: w, height: w)
        layout.sectionInset = UIEdgeInsetsMake(pictureCellMargin, pictureCellMargin, 0, pictureCellMargin)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        super.init(collectionViewLayout: layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        //注册cell
        self.collectionView!.registerClass(pictureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
    }
    
    
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 4
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! pictureCell
        cell.contentView.backgroundColor = UIColor.redColor()
        // Configure the cell
        
        return cell
    }
    //MARK: 自定义cell
    class pictureCell: UICollectionViewCell {
        override init(frame: CGRect) {
            super.init(frame: frame)
            setupUI()
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        //MARK: 设置UI
        private func setupUI(){
            contentView.addSubview(addBtn)
            contentView.addSubview(deleteBtn)
            
            //布局frame
            addBtn.snp_makeConstraints { (make) -> Void in
                make.edges.equalTo(contentView.snp_edges)
            }
            
            deleteBtn.snp_makeConstraints { (make) -> Void in
                make.right.top.equalTo(contentView)
            }
            
        }
        //MARK: 懒加载控件
        private lazy var addBtn : UIButton = UIButton(title: "", backImage: "compose_pic_add", color: nil)
        private lazy var deleteBtn : UIButton = UIButton(title: "", backImage: "compose_photo_close", color: nil)
        
    }
    
}
